<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-banner">
        <h2>加入我们</h2>
        <p>开启您的健康管理之旅</p>
      </div>
      <el-card class="register-form">
        <template #header>
          <div class="card-header">
            <h2>用户注册</h2>
            <p>请填写以下信息</p>
          </div>
        </template>
        <el-form :model="registerForm" :rules="rules" ref="registerFormRef">
          <el-form-item prop="username">
            <el-input 
              v-model="registerForm.username" 
              placeholder="请输入用户名"
              :prefix-icon="User"
              size="large">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input 
              v-model="registerForm.password" 
              type="password" 
              placeholder="请输入密码"
              :prefix-icon="Lock"
              size="large"
              show-password>
            </el-input>
          </el-form-item>
          <el-form-item prop="email">
            <el-input 
              v-model="registerForm.email" 
              placeholder="请输入邮箱"
              :prefix-icon="Message"
              size="large">
            </el-input>
          </el-form-item>
          <el-form-item prop="phone">
            <el-input 
              v-model="registerForm.phone" 
              placeholder="请输入手机号"
              :prefix-icon="Phone"
              size="large">
            </el-input>
          </el-form-item>
          <el-form-item prop="gender">
            <el-select 
              v-model="registerForm.gender" 
              placeholder="请选择性别"
              size="large"
              style="width: 100%">
              <el-option label="男" value="男" />
              <el-option label="女" value="女" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button 
              type="primary" 
              @click="handleRegister" 
              :loading="loading"
              size="large"
              class="submit-btn">
              注册
            </el-button>
          </el-form-item>
          <div class="form-footer">
            <span>已有账号？</span>
            <el-link type="primary" @click="toLogin">立即登录</el-link>
          </div>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Message, Phone } from '@element-plus/icons-vue'
import { useUserStore } from '../stores/user'

const router = useRouter()
const userStore = useUserStore()
const registerFormRef = ref(null)
const loading = ref(false)

const registerForm = reactive({
  username: '',
  password: '',
  email: '',
  phone: '',
  gender: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名长度不能小于3个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性��', trigger: 'change' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  await registerFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await userStore.register(registerForm)
        ElMessage.success('注册成功')
        router.push('/login')
      } catch (error) {
        ElMessage.error(error.message || '注册失败')
      } finally {
        loading.value = false
      }
    }
  })
}

const toLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
}

.register-box {
  display: flex;
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.register-banner {
  width: 400px;
  padding: 40px;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.register-banner h2 {
  font-size: 2.5em;
  margin-bottom: 20px;
}

.register-banner p {
  font-size: 1.2em;
  opacity: 0.9;
}

.register-form {
  width: 400px;
  border: none;
  background: transparent;
}

.card-header {
  text-align: center;
  padding: 0 0 20px 0;
}

.card-header h2 {
  margin: 0;
  font-size: 1.8em;
  color: #303133;
}

.card-header p {
  margin: 10px 0 0 0;
  color: #909399;
}

:deep(.el-input__wrapper) {
  padding: 1px 11px;
}

:deep(.el-input__inner) {
  height: 40px;
}

.submit-btn {
  width: 100%;
  padding: 12px 20px;
  margin-top: 10px;
}

.form-footer {
  text-align: center;
  margin-top: 20px;
  color: #606266;
}

.form-footer .el-link {
  margin-left: 8px;
  font-weight: 600;
}
</style> 